package com.qhg.saas_server.utils;

import javax.servlet.http.HttpServletRequest;

public class IpUtill {
    //获得客户端真实IP地址：
    public static String getIpAddr(HttpServletRequest request) {
        String ip = null;
        java.util.Enumeration enu = request.getHeaderNames();

        while (enu.hasMoreElements()) {
            String name = (String)enu.nextElement();
            if (name.equalsIgnoreCase("X-Real-IP")) {
                System.out.println("cc:x-real-ip="+request.getHeader("X-Real-IP"));
            }
            if (name.equalsIgnoreCase("X-Forwarded-For")) {
                ip = request.getHeader(name);
            }
            else if (name.equalsIgnoreCase("Proxy-Client-IP")) {
                ip = request.getHeader(name);
            }
            else if (name.equalsIgnoreCase("WL-Proxy-Client-IP")) {
                ip = request.getHeader(name);
            }

            if ((ip != null) && (ip.length() != 0)){
                break;
            }
        }
        System.out.println("a:"+ip);
        if ((ip == null) || (ip.length() == 0)){
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
